package yxy.game.pm2.active.command.holiday;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * Author HYB
 * 领取节日签到奖励
 */
@MessageType(proto = Command.ActHolidayDrawRequest, classtype = ActiveMessage.ActHolidayDrawRequest.class)
public class CMD_Active_节日_DrawReward extends Command<ActiveMessage.ActHolidayDrawRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayDrawRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayDrawResponse.Builder response = ActiveMessage.ActHolidayDrawResponse.newBuilder();
        int day = request.getDay();
        if (day > 7 || day < 1) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }
        DrawStatus cangainreward = am.cangainreward(day);
        if (cangainreward == DrawStatus.hadget) {
            send(messageSource, response, Result.ActHadGet);
            return;
        }
        if (cangainreward == DrawStatus.cannotget) {
            send(messageSource, response, Result.ActCannotGet);
            return;
        }
        am.addDrawDay(day);
        Collection<Currency> cur = am.signDrawreward(day);
        if (cur == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        response.addAllDisplay(new CurrencyHandle().packItems(cur));

        //下发道具
        new CurrencyHandle().addCurrency(pcode, CCReason.活动_节日_签到, cur);

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayDrawResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
